package com.apimarket.service;

import com.apimarket.dao.SysRoleMapper;
import com.apimarket.dao.SysRoleMenuMapper;
import com.apimarket.dao.SysUserRoleMapper;
import com.apimarket.entity.ApiSet;
import com.apimarket.entity.SysRole;
import com.apimarket.entity.vo.CheckBox;
import com.apimarket.entity.vo.PageObject;
import com.apimarket.entity.vo.SysRoleMenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.rpc.ServiceException;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public PageInfo<SysRole> findPageObject(String name,
                                             Integer pageCurrent,Integer pageSize) throws ServiceException {
        //1.对参数进行校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码值无效");
        //2.查询总记录数并进行校验
        int rowCount=sysRoleMapper.getRowCount(name);
        System.out.println("rowCount"+rowCount);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");
        //3.查询当前页记录
        //xsc:pageSize是是指定这个页有多少记录？
        int startIndex=(pageCurrent-1)*pageSize;
        PageHelper.startPage(pageCurrent,pageSize);
        List<SysRole> records=
                sysRoleMapper.findPageObjects(name);
        PageInfo<SysRole> pageInfo=new PageInfo<>(records);
        //4.对查询结果进行封装并返回
//        return new PageObject<SysRoleService>(pageCurrent, pageSize,
//                rowCount, records);
        return pageInfo;
    }
//    角色管理删除操作
    public int deleteObject(Integer id) throws ServiceException {
        //1.验证数据的合法性
        if(id==null||id<=0)
            throw new IllegalArgumentException("请先选择");
        //3.基于id删除关系数据
        sysRoleMenuMapper.deleteObjectsByRoleId(id);
        sysUserRoleMapper.deleteObjectsByRoleId(id);
        //4.删除角色自身
        int rows=sysRoleMapper.deleteObject(id);
        if(rows==0)
            throw new ServiceException("此记录可能已经不存在");
        //5.返回结果
        return rows;
    }

    //角色数据添加
    public int saveObject(SysRole entity, Integer[] menuIds) throws ServiceException {
        //1.参数有效性校验
        if(entity==null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }

        if(entity.getName().isEmpty()) {
            throw new IllegalArgumentException("角色名不允许为空");
        }
        if(menuIds==null||menuIds.length==0)
            throw new ServiceException("必须为角色分配权限");
        //2.保存角色自身信息
        int rows=sysRoleMapper.insertObject(entity);
        //3.保存角色菜单关系数据
        sysRoleMenuMapper.insertObjects(entity.getId(), menuIds);
        //4.返回业务结果
        return rows;
    }

    public SysRoleMenuVo findObjectById(Integer id) throws ServiceException {
        //1.合法性验证
        if(id==null||id<=0)
            throw new IllegalArgumentException("id的值不合法");
        //2.执行查询
        SysRoleMenuVo result=sysRoleMapper.findObjectById(id);
        //3.验证结果并返回
        if(result==null)
            throw new ServiceException("此记录已经不存在");
        return result;
    }

    public int updateObject(SysRole entity,Integer[] menuIds) throws ServiceException {
        //1.合法性验证
        if(entity==null)
            throw new IllegalArgumentException("更新的对象不能为空");
        if(entity.getId()==null)
            throw new IllegalArgumentException("id的值不能为空");

        if(entity.getName().isEmpty())
            throw new IllegalArgumentException("角色名不能为空");
        if(menuIds==null||menuIds.length==0)
            throw new IllegalArgumentException("必须为角色指定一个权限");

        //2.更新数据
        int rows=sysRoleMapper.updateObject(entity);
        if(rows==0)
            throw new ServiceException("对象可能已经不存在");
        sysRoleMenuMapper.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuMapper.insertObjects(entity.getId(), menuIds);
        //3.返回结果
        return rows;
    }


    public PageInfo<CheckBox> findObjects(Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<CheckBox> list=sysRoleMapper.findObjects();
        PageInfo<CheckBox> pageInfo=new PageInfo<>(list);

        return pageInfo;
    }
}
